package JavaEE.ManyThread6;
import java.sql.Array;
import java.util.*;
import java.util.concurrent.*;

public class Demo1 {
    public static void main(String[] args) throws InterruptedException{
        //demo1();
        demo2();
    }
    public static void demo1() throws InterruptedException{
        BlockingQueue<String> queue = new LinkedBlockingQueue<>(3);
        // 展示"阻塞队列满时,add()发生阻塞"
        System.out.println("开始执行");
        for(int i = 0;i < 5;i++){
            queue.add("aaa");
            //add()3次后,queue满,阻塞等待take()
        }
        System.out.println("结束执行");//运行不到这里
    }
    public static void demo2() throws InterruptedException{
        BlockingQueue<String> queue = new LinkedBlockingQueue<>(3);
        // 展示"阻塞队列空时,take()发生阻塞"
        System.out.println("开始执行");
        for(int i = 0;i < 5;i++){
            queue.take();
            //queue为空时,take阻塞等待add
        }
        System.out.println("结束执行");//运行不到这里
    }
}

